/**
 * Created by Dalyn on 2017/2/28.
 */
import React from "react";
import { hashHistory, Link } from 'react-router';
import Relay from "react-relay";
// import ListOrderMutation from "./mutation/ListOrderMutation";
import ListEnquiryOrderMutation from "./mutation/ListEnquiryOrderMutation";

import './css/IndentManage.css'
import Util from "../../util/Util"
import Pagination from "./Pagination"
import "./css/ListOrder_SaleAndRe.css"
import { Table,message,Menu, Dropdown, Icon } from 'antd';
import {Button} from 'fly';
import InputBox from "../../common/InputBox";

/*信息列表*/
export default class ListOrder extends React.Component{
    constructor(props){
        super(props);
        this.state={
            orders:[],
            totalresult:0,
            placeholder:'请输入企业名称进行搜索',
            loading:true,
        };
        this.odtail='1';
        this.ods='';
        this.num=1;
        this.enid = sessionStorage.getItem('enterpriseId');
        this.serviceType = sessionStorage.getItem('serviceType');
        this.odsobj={'-1':'待创建',0:'待发布',1:'待接单',2:'待签订',3:'已签订',4:'交易完成'};
        this.toStatus={'-1':'已失效',0:'待发布',1:'待接单',2:'待签订',3:'已签订',4:'交易完成'};
        this.goodtypeobj={'1':'成衣','2':'辅料','3':'面料','4':'印染','5':'纱线'}
    }
    componentWillMount(){
        this.queryorderlist(10,1);
    }
    componentDidMount(){

    }

    queryorderlist(pageSize,pageNum){
        let enName = this.refs.enName ? this.refs.enName.state.text : '';
        let orderType = '';
        if(this.serviceType ==1){
            orderType = '1'
        }else if(this.serviceType ==2){
            orderType = '2'
        }else if(this.serviceType ==3){
            orderType = '3'
        }else if(this.serviceType ==4){
            orderType = '4'
        }else if(this.serviceType ==5){
            orderType = '5'
        }else if(this.serviceType ==6){
        }
        Relay.Store.commitUpdate(
            new ListEnquiryOrderMutation({
                pageSize: pageSize,
                pageNum: pageNum,
                status:'-1,0,1,2',
                type:this.odtail,
                enId:this.enid,
                enName:enName||'',
                searchType:1,
                orderType:orderType
            }),
            {
                onFailure: () => {
                    message.info('请求失败');
                    this.setState({
                        loading:false,
                    })
                },
                onSuccess: (response) => {
                    let errorCode = response.listEnquiryOrder.listEnquiryOrder.errorCode;
                    if(errorCode=='0'){
                        let orders = response.listEnquiryOrder.listEnquiryOrder.orders
                        let totalResult = response.listEnquiryOrder.listEnquiryOrder.totalResult
                        this.setState({
                            orders:orders,
                            totalresult:totalResult,
                            loading:false,
                        });
                    }else {
                        this.setState({
                            loading:false,
                        })
                        if (errorCode == 500){
                            message.error('服务器错误,请联系系统管理员');
                        }
                    }
                }
            }
        )
    }

    reSave(status,oid,enid,type){
        // if(status==2){
        //     return;
        // }
        switch (type){
            case 0:
                return;
            case 1:
                hashHistory.push('/orderdetailgms/'+oid);
                return;
            case 2:
                hashHistory.push('/orderdetailraw/'+oid);
                return;
            case 3:
                hashHistory.push('/orderdetailcloth/'+oid);
                return;
            case 4:
                hashHistory.push('/orderdetailprint/'+oid);
                return;
            case 5:
                hashHistory.push('/orderdetailyarn/'+oid);
                return;

        }
    }


    jumpDetil(oty,oid){
        switch (oty){
            case 0:
                return;
            case 1:
                hashHistory.push('/orderdetailgms/'+oid);
                // hashHistory.push({pathname:'/orderdetailgms/',state:{'oid':oid}})
                return;
            case 2:
                hashHistory.push('/orderdetailraw/'+oid);
                // hashHistory.push({pathname:'/orderdetailraw/',state:{'oid':oid}})
                return;
            case 3:
                hashHistory.push('/orderdetailcloth/'+oid);
                // hashHistory.push({pathname:'/orderdetailcloth/',state:{'oid':oid}})
                return;
            case 4:
                hashHistory.push('/orderdetailprint/'+oid);
                // hashHistory.push({pathname:'/orderdetailprint/',state:{'oid':oid}})
                return;
            case 5:
                hashHistory.push('/orderdetailyarn/'+oid);
                // hashHistory.push({pathname:'/orderdetailprint/',state:{'oid':oid}})
                return;

        }
    }


    addordertyep(orderOperates,oid,enid,type,partB){
       return orderOperates.map((context,index)=>{
            if(context.toId == this.enid){
                if(context.status==1){
                    return <div className="order_list_saleandre_from_td_doitcontext_div"
                                onClick={this.reSave.bind(this,context.status,oid,enid,type)}>{'接单'}</div>
                }else{
                    if(this.enid!=partB){
                        return('已接单');
                    }else{
                        return('已接单');
                    }
                }
            }
         });

    }

    addDepartmenlist(){
        return this.state.orders.map((context,index)=>{
            let date = Util.dateFormat(context.createDate);
            return (
                <thead>
                <tr className={index%2==0?"order_list_saleandre_from_trchange":"order_list_saleandre_from_tr"}>
                    <th className="order_list_saleandre_from_td order_list_saleandre_from_td_oid_contextno" >{context.oid}</th>
                    <th className="order_list_saleandre_from_td order_list_saleandre_from_td_partb">{context.partAName}</th>
                    <th className="order_list_saleandre_from_td order_list_saleandre_from_td_product">{this.goodtypeobj[context.orderType]}</th>
                    <th className="order_list_saleandre_from_td order_list_saleandre_from_td_count">{Number(context.orderGoodsNum).toFixed(3)}</th>
                    <th className="order_list_saleandre_from_td order_list_saleandre_from_td_money">{context.account/100}</th>
                    <th className="order_list_saleandre_from_td order_list_saleandre_from_td_doitcontext">{
                        this.addordertyep(context.orderOperates,context.oid,context.enId,context.orderType,context.partB)

                    }</th>
                    <td className="order_list_saleandre_from_td order_list_saleandre_from_td_endcontext" onClick={this.jumpDetil.bind(this,context.orderType,context.oid)}>查看</td>
                </tr>
                </thead>
            );
        });
    }

    stroke(){

    }

    status(){
        return this.oArr.map((item,index)=>{
            return (
                <span style={{width:80,height:27,borderRadius:"8px",background:"#ccc",display:"block"}} onClick={this.stroke.bind(this)}>{item}</span>
            )
        })
    }

    createorder(){
        hashHistory.push('/createorder')
    }

    onSure(size,num){
        this.num = num;
        this.setState({
            loading:true,
        },this.queryorderlist(size,num));
    }

    onfocusInput(){
        this.setState({
            placeholder:''
        })
    }


    bluronInput(){
        this.setState({
            placeholder:'请输入企业名称进行搜索'
        })

    }

    _SearchByEn(){
        this.queryorderlist(10,1);
        this.refs.pagination.setState({
            text:1
        })
    }

    render(){
        let { orders } = this.state;
        const columns = [
            {
                title: '订单编号',
                dataIndex: 'oid',
                key: 'oid',
                width: 200,
                render: (text, record, index) => {
                    return  (
                        <span title={record.oid} className="order_orderpurch_item_from_td_oid">{record.oid}</span>
                    );
                },
            },
            {
                title: '采购方',
                dataIndex: 'partAName',
                key: 'partAName',
                width: 260,
                render:(text, record) =>{
                    return <span title={record.partAName} className="">{record.partAName}</span>
                }
            },
            {
                title: '商品类型',
                dataIndex: 'orderType',
                key: 'orderType',
                render:(text, record) =>{
                    return <span className="">{this.goodtypeobj[record.orderType]}</span>
                }
            },
            {
                title: '数量',
                dataIndex: 'orderGoodsNum',
                key: 'orderGoodsNum',
                render:(text, record) =>{
                    return <span className="">{Number(record.orderGoodsNum).toFixed(3)}</span>
                }
            },
            {
                title: '订单金额（元）',
                dataIndex: 'account',
                key: 'account',
                render:(text, record) =>{
                    return <span className="">{record.account/100}</span>
                }
            },
            {
                title: '订单状态',
                dataIndex: 'status',
                key: 'status',
                render:(text, record, index) =>{
                    if(record.isValid == 0){
                        return <span className="">已失效</span>
                    }else{
                        if(record.status == 2){
                            /*其他乙方已接单-己方未接*/
                            let status = null;
                            let orderOperates = record.orderOperates?record.orderOperates:[];
                            orderOperates.forEach((v,i)=>{

                                if(v.toId == this.enid){
                                    status = v.status;
                                    return;
                                }
                            });
                            return <span className="">{this.toStatus[status]}</span>
                        }else if(record.status >= 3){
                            /*订单已签订-但不是和已方*/
                            if(record.partB == this.enid){
                                return <span className="">{this.odsobj[record.status]}</span>
                            }else {
                                return <span className="">已失效</span>
                            }
                        }else {
                            return <span className="">{this.odsobj[record.status]}</span>
                        }
                    }
                }
            },
            {
                title: '操作',
                dataIndex: 'operate',
                key: 'operate',
                width: 100,
                render:(text, record) =>{
                    const fun = {
                        jumpDetail:()=>{
                            hashHistory.push({pathname:'/OrderIndex/',state:{
                                'record':record,
                                "defaultActiveKey":1,
                            }})
                        },
                    };

                    const menuItem = [
                        {key:"receive",iconType:"database",text:"接单",onClick:()=>{fun.jumpDetail();}},
                        {key:"detail",iconType:"file-text",text:"查看详情",onClick:()=>{fun.jumpDetail()}},
                    ];
                    const menu = (
                        <Menu
                            className="listOrder_purchase_dropDown_menu"
                        >
                            {
                                menuItem.map((v,i)=>{
                                    if(record.isValid == 0){
                                        {/*已失效-跳转到详情*/}
                                        if(v.key == "detail"){
                                            return (
                                                <Menu.Item key={v.key}>
                                                    <span className="listOrder_purchase_dropDown_menuItem" onClick={v.onClick}><Icon type={v.iconType} className="listOrder_purchase_dropDown_menuItem_icon"/>{v.text}</span>
                                                </Menu.Item>
                                            )
                                        }
                                    }else {
                                        if(record.status == 1){
                                            {/*待接单-已发布*/}
                                            if(v.key == "receive"){
                                                return (
                                                    <Menu.Item key={v.key}>
                                                        <span className="listOrder_purchase_dropDown_menuItem" onClick={v.onClick}><Icon type={v.iconType} className="listOrder_purchase_dropDown_menuItem_icon"/>{v.text}</span>
                                                    </Menu.Item>
                                                )
                                            }
                                        }else if(record.status == 2 ){
                                            let receiveFlag = null;
                                            let orderOperates = record.orderOperates?record.orderOperates:[];
                                            orderOperates.forEach((v,i)=>{
                                                if(v.toId == this.enid){
                                                    if(v.status == 1){
                                                        {/*待接单-已发布*/}
                                                        receiveFlag = true;
                                                    }else if(v.status == 2){
                                                        {/*已接单*/}
                                                        receiveFlag = false;
                                                    }
                                                    return
                                                }
                                            });
                                            if(receiveFlag){
                                                {/*待接单-已发布*/}
                                                if(v.key == "receive"){
                                                    return (
                                                        <Menu.Item key={v.key}>
                                                            <span className="listOrder_purchase_dropDown_menuItem" onClick={v.onClick}><Icon type={v.iconType} className="listOrder_purchase_dropDown_menuItem_icon"/>{v.text}</span>
                                                        </Menu.Item>
                                                    )
                                                }
                                            }else {
                                                {/*已接单*/}
                                                if(v.key == "detail"){
                                                    return (
                                                        <Menu.Item key={v.key}>
                                                            <span className="listOrder_purchase_dropDown_menuItem" onClick={v.onClick}><Icon type={v.iconType} className="listOrder_purchase_dropDown_menuItem_icon"/>{v.text}</span>
                                                        </Menu.Item>
                                                    )
                                                }
                                            }
                                        }else{
                                            {/*已失效-跳转到详情*/}
                                            if(v.key == "detail"){
                                                return (
                                                    <Menu.Item key={v.key}>
                                                        <span className="listOrder_purchase_dropDown_menuItem" onClick={v.onClick}><Icon type={v.iconType} className="listOrder_purchase_dropDown_menuItem_icon"/>{v.text}</span>
                                                    </Menu.Item>
                                                )
                                            }
                                        }
                                    }
                                })
                            }
                        </Menu>
                    );
                    return (
                        <Dropdown overlay={menu} placement="bottomLeft">
                            <Icon type="ellipsis" className="listOrder_purchase_dropDown_icon"/>
                        </Dropdown>
                    )
                }
            },
        ];
        return (
            <div className="order_box">
                <div className="order_saleandre_frome_search_box">
                    <div style={{display:"inline-block",marginLeft:'20px',marginRight: '-3px'}}>
                        <InputBox className="order_saleandre_frome_search_input pro_overflow_inputSty "
                                  space={20}
                                  ref={"enName"}
                                  defaultvalute="请输入企业名称"
                                  changeon={this.bluronInput.bind(this,"enName")}
                        />
                    </div>
                    <Button onClick={this._SearchByEn.bind(this)}>搜索</Button>
                    {/*<div className="order_saleandre_frome_search_div" onClick={this._SearchByEn.bind(this)}>搜索</div>*/}
                </div>

                <div style={{height:"300px",background:"#fff",marginTop:"20px",padding:"0 20px 0",position:"relative"}}>
                    {/*修改-0602*/}
                    <Table
                        columns={columns}
                        dataSource={orders}
                        style={{width:'100%',marginTop:"20px",margin:"0 auto"}}
                        className="order_table"
                        pagination={false}
                        rowKey={record => record.oid}
                        loading={this.state.loading}
                    />
                    <Pagination eachSize={10} dataSize={this.state.totalresult} onSure={this.onSure.bind(this)} ref={'pagination'}/>
                </div>
            </div>
        )
    }
}
